package study.ct.com.tvwebviewapplication;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

import study.ct.com.tvwebviewapplication.utils.MyToast;

import static android.text.TextUtils.isEmpty;


/**
 * Created by Administrator on 2016/8/15 0015.
 */
public class WebViewActivity extends Activity {

    public static final String UrlKey = "url";
    private String number = "";
    private WebView webView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.web_view);

        initData();
        initView();

        Intent intent = getIntent();
        String url = intent.getStringExtra(UrlKey);
        Log.i("ct", "intent url=" + url);

        String model=getSystemModel();
        String deviceBrand=getDeviceBrand();
        Log.i(TAG, "onCreate: model=="+model+"" +
                ";;brand=="+deviceBrand);


        webView.getSettings().setJavaScriptEnabled(true);
        // 设置允许JS弹窗
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setBuiltInZoomControls(false);
        webView.getSettings().setPluginState(WebSettings.PluginState.ON);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setDomStorageEnabled(true);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // progressBar.show();
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {


            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            }
        });

        webView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                progressBar.setProgress(newProgress);

                if (newProgress > 98) {
                    progressBar.setVisibility(View.GONE);
                } else {
                    progressBar.setVisibility(View.VISIBLE);
                }
                super.onProgressChanged(view, newProgress);
            }
        });

        loadUrl(url);
    }

    private static final String TAG = "WebViewActivity";
    public void initData(){
        Intent intent = getIntent();
        number = intent.getStringExtra("info1");
        // MyToast.showToast(MainActivity.this, "number===" + number);
        if (number == null || number == "") {
            number = "i520479456@itv";//i5203281454@itv  i5203281454@itv
            MyToast.showToast(this, "首次安装后不能自动获取账户信息登录，请退出电视营业厅后再次进入");
        }
    }

    private ProgressBar progressBar;
    private TextView keycode;

    public void initView() {
        keycode = findViewById(R.id.keycode);
        webView = (WebView) findViewById(R.id.wv_webview);
        progressBar = findViewById(R.id.progress);
    }


    public void loadUrl(String url) {

        // 通过addJavascriptInterface()将Java对象映射到JS对象
        //参数1：Javascript对象名
        //参数2：Java对象名
        webView.addJavascriptInterface(new AndroidtoJs(), "tv");//AndroidtoJS类对象映射到js的test对象

        if (webView != null) {
            Log.i("loadUrl", "url===" + url);
            webView.loadUrl(url);

        }
    }

    public class AndroidtoJs extends Object {

        // 定义JS需要调用的方法
        // 被JS调用的方法必须加入@JavascriptInterface注解
        @JavascriptInterface
        public void hello(String msg) {
            Log.i(TAG, "hello: JS调用了Android的hello方法");
        }

        @JavascriptInterface
        public String getUserId(String token){
            return number;
        }

        @JavascriptInterface
        public String getDeviceName(String token){
            String device = getSystemModel();
            Log.i(TAG, "getDeviceMes: device_name==="+device);
            return device;
        }

        @JavascriptInterface
        public String getDeviceBrandName(String token){
            String deviceBrand = getDeviceBrand();
            Log.i(TAG, "getDeviceMes: getDeviceBrand==="+deviceBrand);
            return deviceBrand;
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.i("ct", "onKeyDown  keyCode===" + keyCode);

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Log.i("ct", "onKeyDown KEYCODE_BACK progressBar=");


            boolean canGoback = webView.canGoBack();
            Log.i("ct", "onKeyDown canGoback=" + canGoback);
            if (canGoback) {
                webView.goBack(); // goBack()表示返回WebView的上一页面
                return true;
            } else {
                finish();

            }
        }

        return false;
    }

    /**
     * 获取手机型号
     * @return  手机型号
     */
    public  String getSystemModel() {
        return android.os.Build.MODEL;
    }

    /**
     * 获取手机厂商
     *
     * @return  手机厂商
     */
    public  String getDeviceBrand() {
        return android.os.Build.BRAND;
    }
}
